package com.xbongbong.pro.message.pushset;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTimeTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 刘阳
 * @date 2019/9/10 3:29 PM
 * @since v1.0
 * @version v1.0
 */
public class QuotationRemindPushSet extends AbstractPushSet {

    /**{
     "type":"2",
     "manager": {			//推送给上级主管
     "enable": 0,
     "value": ["2","5"]	//表示主管层级
     },
     "user": {				推送的指定人
     "enable": 0,
     "value": []
     }
     }
     */
    private String pushObject;

    /**{
     "time": -1,			//每天几点推送 00：00 + 32400秒 即8:00PM推送
     "period": 0,
     "timeIntervalType": 0,	//每天推送
     "type": 3				//循环推送
     }
     */
    private String pushTime;

    private static final Integer ONE_DAY = 86400;
    private static final Integer THREE_DAY = 3*86400;
    private static final Integer FIVE_DAY = 5*86400;
    private static final Integer SEVEN_DAY = 7*86400;
    private static final Integer FIFTEEN_DAY = 15*86400;

    private static final Integer ONE_DAY_TYPE = 1;
    private static final Integer THREE_DAY_TYPE = 2;
    private static final Integer FIVE_DAY_TYPE = 3;
    private static final Integer SEVEN_DAY_TYPE = 4;
    private static final Integer FIFTEEN_DAY_TYPE = 5;

    public String getPushObject() {
        return pushObject;
    }


    public void setPushObject(String pushObject) {
        this.pushObject = pushObject;
    }

    public String getPushTime() {
        return pushTime;
    }


    public void setPushTime(String pushTime) {
        this.pushTime = pushTime;
    }


    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime,
                                         String attrEntity) {
        setPushObject(pushObject);
        setPushTime(pushTime);

        PushSetEntity pushSetEntity = new PushSetEntity();

        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);

        return pushSetEntity;
    }



    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType,
                                    PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.QUOTATION_REMIND.getCode();

        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("subType", subType);
        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        PushSetEntity pushSetEntity;
        if(pushSetList.isEmpty()){
            pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);
            pushSetEntity.setEnable(1);
            JSONObject pushObject = new JSONObject();
            if (Objects.equals(PushTypeEnum.QUOTATION_REMIND.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.QUOTATION_REMIND.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.QUOTATION_REMIND));
                //type
                JSONArray typeArray = new JSONArray();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", PushObjectTypeEnum.MAINUSER.getName());
                jsonObject.put("enable", 1);
                jsonObject.put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                typeArray.add(jsonObject);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                jsonObject1.put("enable", 1);
                jsonObject1.put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                typeArray.add(jsonObject1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("name", PushObjectTypeEnum.QUOTATION_PERSON.getName());
                jsonObject2.put("enable", 1);
                jsonObject2.put("alias", PushObjectTypeEnum.QUOTATION_PERSON.getAlias());
                typeArray.add(jsonObject2);
                pushObject.put("type", typeArray);
                //manager
                JSONObject managerObject = new JSONObject();
                managerObject.put("enable", 0);
                managerObject.put("value", new ArrayList<>());
                pushObject.put("manager", managerObject);
                //dep
                JSONObject depObject = new JSONObject();
                depObject.put("enable", 0);
                depObject.put("value", new ArrayList<>());
                pushObject.put("dep", depObject);
                //role
                JSONObject roleObject = new JSONObject();
                roleObject.put("enable", 0);
                roleObject.put("value", new ArrayList<>());
                pushObject.put("role", roleObject);
                //user
                JSONObject userObject = new JSONObject();
                userObject.put("enable", 0);
                userObject.put("value", new ArrayList<>());
                pushObject.put("user", userObject);

                //pushTime
                JSONObject pushTimeObject = new JSONObject();
                pushTimeObject.put("time", 0);
                pushTimeObject.put("period", QuotationRemindPushSet.ONE_DAY);
                pushTimeObject.put("loop", 0);
                pushTimeObject.put("timeType", PushTimeTypeEnum.ADVANCE.getCode());
                pushTimeObject.put("type", 1);

                JSONArray timeArray = new JSONArray();
                JSONObject timeObject = new JSONObject();
                timeObject.put("id", 1);
                timeObject.put("name", I18nMessageUtil.getMessage(CommonConstant.ADVANCE));
                timeArray.add(timeObject);
                JSONObject timeObject2 = new JSONObject();
                timeObject2.put("id", 3);
                timeObject2.put("name", I18nMessageUtil.getMessage(CommonConstant.THE_DAY));
                timeArray.add(timeObject2);
                pushTimeObject.put("timeArray", timeArray);

                pushSetEntity.setPushTime(pushTimeObject.toJSONString());
            }
            pushSetEntity.setPushObject(pushObject.toJSONString());
        } else {
            pushSetEntity = pushSetList.get(0);
            pushSetEntity.setTitle(PushTypeEnum.QUOTATION_REMIND.getName());
            pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.QUOTATION_REMIND));
        }
        return pushSetEntity;
    }



    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        Integer timeType = PushTimeTypeEnum.ADVANCE.getCode();
        Integer time = -1;
        Integer period = 0;
        Integer timeIntervalType = 0;
        Integer loop = 0;
        Integer pushTimeType = jsonObject.getInteger("pushTimeType");
        if(Objects.equals(pushTimeType, ONE_DAY_TYPE)){
            period = ONE_DAY;
        }else if(Objects.equals(pushTimeType, THREE_DAY_TYPE)){
            period = THREE_DAY;
        }else if(Objects.equals(pushTimeType, FIVE_DAY_TYPE)){
            period = FIVE_DAY;
        }else if(Objects.equals(pushTimeType, SEVEN_DAY_TYPE)){
            period = SEVEN_DAY;
        }else if(Objects.equals(pushTimeType, FIFTEEN_DAY_TYPE)){
            period = FIFTEEN_DAY;
        }
        JSONObject retObject = new JSONObject();
        retObject.put("timeType", timeType);
        retObject.put("time", time);
        retObject.put("period", period);
        retObject.put("timeIntervalType", timeIntervalType);
        retObject.put("loop", loop);
        return retObject;
    }


    @Override
    public PushSetEntity getPushSet(String corpid, String attr,
                                    PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }
}
